import 'package:flutter/material.dart';

class TabBarWidget extends StatefulWidget {
  static const int BOTTOM_TAB = 1;

  static const int TOP_TAB = 2;

  final int type;
  final List<Widget> tabItems;
  final List<Widget> tabViews;
  final Color backgroundColor;
  final Color indicatorColor;
  final Widget title;
  final Widget drawer;
  final Widget flaoatingActionButton;
  final PageController pageController;

  TabBarWidget({Key key, this.type, this.tabItems, this.tabViews, this.backgroundColor, this.indicatorColor,
    this.title, this.drawer, this.flaoatingActionButton, this.pageController}) : super(key: key);

  @override
  _TabBarWidgetState createState()=>new _TabBarWidgetState(type,tabItems,tabViews,backgroundColor,indicatorColor,title,
  drawer,flaoatingActionButton,pageController);

}

class _TabBarWidgetState extends State<TabBarWidget>
    with SingleTickerProviderStateMixin {
  final int _type;
  final List<Widget> _tabItems;
  final List<Widget> _tabViews;
  final Color _backgroundColor;
  final Color _indicatorColor;
  final Widget _title;
  final Widget _drawer;
  final Widget _flaoatingActionButton;
  final PageController _pageController;

  _TabBarWidgetState(
      this._type,
      this._tabItems,
      this._tabViews,
      this._backgroundColor,
      this._indicatorColor,
      this._title,
      this._drawer,
      this._flaoatingActionButton,
      this._pageController)
      : super();

  TabController _tabController;

  @override
  void initState() {
    ///初始化时创建控制器
    ///通过 with SingleTickerProviderStateMixin 实现动画效果。
    super.initState();
    _tabController = new TabController(length: _tabItems.length, vsync: this);
  }

  @override
  void dispose() {
    ///页面销毁时，销毁控制器
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    if (this._type == TabBarWidget.TOP_TAB) {
      return new Scaffold(
        drawer: _drawer,
        floatingActionButton: _flaoatingActionButton,
        appBar: new AppBar(
          backgroundColor: _backgroundColor,
          title: _title,
          bottom: new TabBar(
            tabs: _tabItems,
            isScrollable: true,
            controller: _tabController,
            indicatorColor: _indicatorColor,
          ),
          elevation: 0.0,
        ),
        body: new PageView(
          controller: _pageController,
          children: _tabViews,
          onPageChanged: (index) {
            _tabController.animateTo(index);
          },
        ),
      );
    }
    return new Scaffold(
      drawer: _drawer,
      floatingActionButton: _flaoatingActionButton,
      appBar: new AppBar(
        backgroundColor: _backgroundColor,
        title: _title,
      ),
      body: new PageView(
        controller: _pageController,
        children: _tabViews,
        onPageChanged: (index) {
          _tabController.animateTo(index);
        },
      ),
      bottomNavigationBar: new Material(
        color: _backgroundColor,
        child: new TabBar(
          tabs: _tabItems,
          controller: _tabController,
          indicatorColor: _indicatorColor,
        ),
      ),
    );
  }
}

class TarWidgetControl {
  List<Widget> footerButton = [];
}
